package com.michaellou.report;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

public class Report {

	 static int twoWeekAvg=3557;
	 static int twoWeekAvg_xx=3557;
	
	static SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*
		 1,100000300,100000600,100001000,100001900,100002200,100002300,100002500,100003200,101,102,104,106,108,113,121,122,146,16,188,193,2,200012500,30,4,44,48,50,576,579,582,585,587,591,60,61,615,64,662,671,69,70,710,73,777,78,79,88,89,91,913,93
		 100001700,109,11,116,120,13,14,142,15,17,201027000,39,41,45,5,631,661,92,96,99999985
		 
		 */
//		System.out.println("----------------mir2----------------");
//		calRatio(new int[]{4});
//		System.out.println("----------------woool----------------");
//		calRatio(new int[]{1});
		
		//new int[]{188};//
		int[] mmolist=new int[]{1,2,4,39,44,48,50,61,70,73,78,79,88,89,91,93,101,106,108,121,142,146,188,576,579,582,585,615,710,100000600,100002300,200012500};
			//new int[]{1,100000300,100000600,100001000,100001900,100002200,100002300,100002500,100003200,101,102,104,106,108,113,121,122,146,16,188,193,2,200012500,30,4,44,48,50,576,579,582,585,587,591,60,61,615,64,662,671,69,70,710,73,777,78,79,88,89,91,913,93};
		int[] xxlist=new int[]{5,11,13,17,45,109,661,116,14};
			//new int[]{100001700,109,11,116,120,13,14,142,15,17,201027000,39,41,45,5,631,661,92,96,99999985};
		
		
//		twoWeekAvg=getLast2weekAvg(mmolist);
//		twoWeekAvg_xx=getLast2weekAvg(xxlist);
		
//		int gameId=188;
		System.out.println("----------------mmo----------------");
		calRatio(mmolist);
		System.out.println("----------------xiuxian----------------");
		calRatio(xxlist);
	}

	private static int getLast2weekAvg(int[] list){
		try {
			Long t1=sf.parse("2012-09-16 00:00:00").getTime()/1000;
			Long t2=sf.parse("2012-09-30 00:00:00").getTime()/1000;
			
			int sum=0;
			for(int gameId:list){
				String url="http://116.211.1.183:8080/soanew!jsonData.soa?ProjectID="+gameId+"&StartTime="+t1
					+"&EndTime="+t2
					+"&Opt=avg";
				int rs=exeJson(url);
				System.out.println("rs:"+rs);
				sum+=rs;
			}
			return sum;
//			System.out.println(t1+" "+t2);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	private static void calRatio(int[] mmolist) {
		
		Calendar cal=Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		Date today=cal.getTime();
		cal.add(Calendar.DAY_OF_MONTH, 1);
		Date todayEnd=cal.getTime();
		System.out.println("today:"+sf.format(today)+" end:"+sf.format(todayEnd));
		
		cal.add(Calendar.DAY_OF_MONTH, -2);
		Date yesterday=cal.getTime();
		System.out.println("yesterday:"+sf.format(yesterday)+" end:"+sf.format(today));
		
		cal.add(Calendar.DAY_OF_MONTH, -7);
		Date weekAgo=cal.getTime();
		cal.add(Calendar.DAY_OF_MONTH, 1);
		Date weekAgoEnd=cal.getTime();
		System.out.println("weekAgo:"+sf.format(weekAgo)+" end"+sf.format(weekAgoEnd));
		
		cal.setTime(today);
		cal.set(Calendar.HOUR_OF_DAY, 15);
		Date today_15=cal.getTime();
		System.out.println("today_15:"+sf.format(today_15));
		
		cal.add(Calendar.DAY_OF_MONTH, -7);
		Date week_15=cal.getTime();
		System.out.println("week_15:"+sf.format(week_15));
		
		int preSum=0;
		int yesSum=0;
		int[] gameList=mmolist;
		for(int gameId:gameList){
			int rs=handleGame(gameId,today,todayEnd,weekAgo,weekAgoEnd,yesterday,today_15,week_15);
			if(0<rs){
				preSum+=rs;
			}
		}
		System.out.println("--------------------------last 2 week------------------------");
		twoWeekAvg=getLast2weekAvg(mmolist);
//		twoWeekAvg_xx=getLast2weekAvg(xxlist);
		//calculate the ratio
		System.out.println("--------------------------twoWeekAvg:"+twoWeekAvg+"------------------------");
		int v=0;
		if(0<preSum){
			v=(Math.abs(preSum-twoWeekAvg)*100/twoWeekAvg);
		}
		String value=""+v;
		if(yesSum>preSum){
			value="-"+value;
		}
		System.out.println("---preSum:"+preSum+"------["+value+"%]---");
	}
	
	private static int handleGame(int gameId,Date today,Date todayEnd,Date weekAgo,Date weekAgoEnd,Date yesterday,Date today_15,Date week_15){
		
		System.out.println("----------------gameId:"+gameId+"------------------");
		Long start=1332000641L*1000;
		Long end=1332400641L*1000;
		
		start=today.getTime()/1000;
		end=todayEnd.getTime()/1000;
		Integer max=getMax(gameId, start, end);
//		int avg=getAvg(gameId, start, end);
		
		System.out.println("today:"+"max:"+max);
		
		start=weekAgo.getTime()/1000;
		end=weekAgoEnd.getTime()/1000;
		Integer max_old=getMax(gameId, start, end);
		Integer avg_old=getAvg(gameId, start, end);
		
		System.out.println("weekAgo:"+"max:"+max_old+" avg:"+avg_old);
		
		
		int today_15_num=getNum(gameId,  today_15.getTime()/1000, today_15.getTime()/1000+60);
		
		int week_15_num=getNum(gameId,  week_15.getTime()/1000, week_15.getTime()/1000+60);
		
		System.out.println("today_15_num:"+today_15_num+" week_15_num:"+week_15_num);
		
		Integer avg_pre=0;
		if(0<max_old){
			BigDecimal d1=new BigDecimal(today_15_num);
			BigDecimal d2=new BigDecimal(week_15_num);
			BigDecimal d3=new BigDecimal(avg_old);
			
			d1=d1.multiply(d3);
			d1=d1.divide(d2,BigDecimal.ROUND_FLOOR);
			avg_pre=d1.toBigInteger().intValue();
//			avg_pre=avg_old*(max/max_old);
		}
		System.out.println("predict:avg:"+avg_pre);
		
//		start=yesterday.getTime()/1000;
//		end=today.getTime()/1000;
//		int avg_yes=getAvg(gameId, start, end);
//		System.out.println("avg_pre:"+avg_pre+" yesterday avg:"+avg_yes);
		
		
		
		
		return avg_pre;
	}
	
	
	
	private static int getMax(int gameId, Long start, Long end) {
		return getCommon(gameId,start,end,"max");
	}
	private static int getAvg(int gameId, Long start, Long end) {
		return getCommon(gameId,start,end,"avg");
	}
	private static int getCommon(int gameId, Long start, Long end,String type) {
		String url="http://116.211.1.183:8080/soanew!jsonData.soa?ProjectID="+gameId
			+"&StartTime="+start
			+"&EndTime="+end
			+"&Opt="+type;
		
//		System.out.println(CommonHttpClient.doGet(url));
//		System.out.println(url);
		
		  return exeJson(url);
	}

	private static int exeJson(String url) {
		String con=CommonHttpClient.doGet(url);
		
		Num bean=null;
		if(null!=con&&con.length()>1){
			con=con.substring(1, con.length()-1);
			
			JSONObject jsonObject = JSONObject.fromObject(con);      
			
			Map<String, Class<Data>> classMap = new HashMap<String, Class<Data>>(); 
			classMap.put("data", Data.class); 
			bean = (Num)JSONObject.toBean( jsonObject,Num.class , classMap); 
		}
		  if(null!=bean){
//			  System.out.println(bean.getTitle());
			  if(null!=bean.getData()){
				  for(Data data:bean.getData()){
					 return data.getNum();
				  }
			  }
//			  System.out.println("max:"+max+" avg:"+avg+" sum:"+sum);
		  }
		  return 0;
	}

	
	private static int getNum(int gameId, Long start, Long end) {
		String url="http://116.211.1.183:8080/soanew!jsonData.soa?ProjectID="+gameId
			+"&PixelUnit=M&Pixel=1&StartTime="+start
			+"&EndTime="+end
			+"&Offset=1";
		
//		System.out.println(CommonHttpClient.doGet(url));
//		String con=CommonHttpClient.doGet(url);
//		System.out.println("con:"+con);
		
		return exeJson(url);
		
//		Num bean=null;
//		if(null!=con&&con.length()>1){
//			con=con.substring(1, con.length()-1);
//			
//			JSONObject jsonObject = JSONObject.fromObject(con);      
//			
//			Map<String, Class<Data>> classMap = new HashMap<String, Class<Data>>(); 
//			classMap.put("data", Data.class); 
//			bean = (Num)JSONObject.toBean( jsonObject,Num.class , classMap); 
//		}
////		int max=0;
////		int avg=0;
////		int sum=0;
//		  if(null!=bean){
////			  System.out.println(bean.getTitle());
//			  for(Data data:bean.getData()){
////				  System.out.println("time:"+data.getTime()+" num:"+data.getNum());
//				  return data.getNum();
//			  }
////			  System.out.println("max:"+max+" avg:"+avg+" sum:"+sum);
//		  }
//		  return 0;
	}

//	private static int[] getNum(int gameId, Long start, Long end) {
//		String url="http://116.211.1.183:8080/soanew!jsonData.soa?ProjectID="+gameId
//			+"&PixelUnit=M&Pixel=1&StartTime="+start
//			+"&EndTime="+end
//			+"&Offset=1";
//		
////		System.out.println(CommonHttpClient.doGet(url));
//		String con=CommonHttpClient.doGet(url);
////		System.out.println("con:"+con);
//		
//		Num bean=null;
//		if(null!=con&&con.length()>1){
//			con=con.substring(1, con.length()-1);
//			
//			JSONObject jsonObject = JSONObject.fromObject(con);      
//			
//			Map<String, Class<Data>> classMap = new HashMap<String, Class<Data>>(); 
//			classMap.put("data", Data.class); 
//			bean = (Num)JSONObject.toBean( jsonObject,Num.class , classMap); 
//		}
//		int max=0;
//		int avg=0;
//		int sum=0;
//		  if(null!=bean){
//			  System.out.println(bean.getTitle());
//			  for(Data data:bean.getData()){
////				  System.out.println("time:"+data.getTime()+" num:"+data.getNum());
//				  sum+=data.getNum();
//				  if(max<data.getNum()){
//					  max=data.getNum();
//				  }
//			  }
//			  if(0<bean.getData().size()){
//				  avg=sum/bean.getData().size();
//			  }
////			  System.out.println("max:"+max+" avg:"+avg+" sum:"+sum);
//		  }
//		  
//		  return new int[]{max,avg};
//	}

}
